<template>
  <div>
    <!-- 面包屑导航栏 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>参数列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 提醒显示内容 -->
      <el-alert title="注意：只允许为第三级分类设置相关参数！" type="warning" show-icon></el-alert>
      <!-- 级联选择器 -->
      <div class="block">
        <span class="demonstration">选择商品分类：</span>
        <!-- <el-cascader
          v-model="argument"
          :options="categoriesData"
          :props="{expandTrigger: 'hover',value:'cat_id',label:'cat_name'}"
          @change="handleChange"
        ></el-cascader>-->

        <cascader ref="cascaderhaha" :propsP="propsP" :activeName="activeName" @cascaderFun="show"></cascader>
        <!-- tabs标签栏 -->
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <!-- 添加动态参数 -->
          <el-tab-pane label="动态参数" name="many">
            <el-button
              type="primary"
              :disabled="isBtnDisabled"
              @click="addCategoriesVisible = true"
            >添加参数</el-button>
            <!-- 动态参数表格 -->
            <el-table :data="paramList" style="width: 100%" border>
              <el-table-column type="expand">
                <template slot-scope="scope">
                  <!-- 动态参数列表 -->
                  <el-tag
                    style="margin: 0 10px"
                    :key="i"
                    v-for="(item, i) in scope.row.attr_vals"
                    closable
                    :disable-transitions="false"
                    @close="handleClose(scope.row,i)"
                  >{{item}}</el-tag>
                  <el-input
                    style="width:30%"
                    class="input-new-tag"
                    v-if="scope.row.inputVisible"
                    v-model="scope.row.inputValue"
                    ref="saveTagInput"
                    size="small"
                    @keyup.enter.native="handleInputConfirm(scope.row)"
                    @blur="handleInputConfirm(scope.row)"
                  ></el-input>
                  <el-button
                    v-else
                    class="button-new-tag"
                    size="small"
                    @click="showInput(scope.row)"
                  >+ New Tag</el-button>
                </template>
              </el-table-column>
              <el-table-column type="index"></el-table-column>
              <el-table-column property="attr_name" label="参数名称"></el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="primary"
                    icon="el-icon-edit"
                    @click="editConfime(scope.row)"
                  >编辑</el-button>
                  <el-button
                    size="mini"
                    type="danger"
                    icon="el-icon-delete"
                    @click="deleteConfime(scope.row.attr_id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>

          <!-- 添加静态属性 -->
          <el-tab-pane label="静态属性" name="only">
            <el-button type="primary">添加属性</el-button>
            <!-- 静态属性表格 -->
            <el-table :data="paramList" style="width: 100%" border>
              <el-table-column type="expand">
                <template slot-scope="scope">
                  <!-- 静态属性列表 -->
                  <el-tag
                    style="margin: 0 10px"
                    :key="i"
                    v-for="(item, i) in scope.row.attr_vals"
                    closable
                    :disable-transitions="false"
                    @close="handleClose(scope.row,i)"
                  >{{item}}</el-tag>
                  <el-input
                    style="width:30%"
                    class="input-new-tag"
                    v-if="scope.row.inputVisible"
                    v-model="scope.row.inputValue"
                    ref="saveTagInput"
                    size="small"
                    @keyup.enter.native="handleInputConfirm(scope.row)"
                    @blur="handleInputConfirm(scope.row)"
                  ></el-input>
                  <el-button
                    v-else
                    class="button-new-tag"
                    size="small"
                    @click="showInput(scope.row)"
                  >+ New Tag</el-button>
                </template>
              </el-table-column>
              <el-table-column type="index"></el-table-column>
              <el-table-column property="attr_name" label="参数名称"></el-table-column>
              <!-- 编辑删除功能操作 -->
              <el-table-column label="操作">
                <template>
                  <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
                  <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-card>
    <!-- 点击添加按钮弹出对话框 -->
    <el-dialog
      :title="'添加'+ titleText"
      :visible.sync="addCategoriesVisible"
      width="70%"
      @close="editDialogClosed"
    >
      <!-- 输入的内容 -->
      <el-form
        :model="addCategoriesForm"
        ref="addCategoriesRef"
        label-width="100px"
        class="demo-ruleForm"
        :rules="rules"
      >
        <el-form-item label="名称" prop="attr_name">
          <el-input v-model="addCategoriesForm.attr_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCategoriesVisible = false">取 消</el-button>
        <el-button type="primary" @click="addParamsConfirm">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 点击修改按钮弹出对话框 -->
    <el-dialog
      :title="'修改'+titleText"
      :visible.sync="editCategoriesVisible"
      width="70%"
      @close="editDialogClosed"
    >
      <!-- 输入的内容 -->
      <el-form
        :model="editForm"
        ref="addCategoriesRef"
        label-width="100px"
        class="demo-ruleForm"
        :rules="rules"
      >
        <el-form-item label="参数名称" prop="attr_name">
          <el-input v-model="editForm.attr_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editCategoriesVisible = false">取 消</el-button>
        <el-button type="primary" @click="editParamsConfirm(editForm)">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
           
<script>
import cascader from "@/components/common/cascader.vue";
import {
  categoriesList,
  paramsList,
  addCategories,
  deleteParams,
  editParams
} from "@/api";
export default {
  data() {
    return {
      //选择的状态不同获取不同的数据
      activeName: "many",
      //选择分类获取到的id
      argument: [],
      // 商品分类列表
      categoriesData: [],
      propsP: { expandTrigger: "hover", value: "cat_id", label: "cat_name" },
      //动态参数列表  静态
      paramList: [],
      //分类id
      categoriesId: "",
      //添加用户参数
      addCategoriesForm: {
        attr_name: "111",
        attr_sel: ["many"]
      },
      // 修改的表单数据对象
      editForm: {},
      //点击显示input框还是不显示
      // inputVisible: false,
      // inputValue: "",
      // 规则设置
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" }
        ]
      },
      addCategoriesVisible: false,
      editCategoriesVisible: false
    };
  },
  methods: {
    //子组件返回的参数
    show(val) {
      this.argument = val.argument;
      this.categoriesData = val.categoriesData;
      this.paramList = val.paramList;
    },

    //点击编辑关闭按钮
    editDialogClosed() {
      this.$refs.addCategoriesRef.resetFields();
    },
    //tab页点击事件触发的函数
    handleClick(tab, event) {
      this.activeName = tab.name;
      // console.log(tab.name,11);
      // this.handleChange();
      // this.$refs.cascaderhaha.handleChange();
    },
    // 点击添加参数按钮转换成输入框
    showInput(row) {
      row.inputVisible = true;
      row.inputValue = "";
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    //失去焦点或鼠标弹起事件
    handleInputConfirm(row) {
      console.log(row, 88);
      let inputValue = row.inputValue;
      if (inputValue) {
        console.log(inputValue, 91);
        row.attr_vals.push(inputValue);
        this.editParamsConfirm(row);
      }
      row.inputVisible = false;
      row.inputValue = "";
    },
    // 点击确定按钮添加参数
    async addParamsConfirm() {
      var { data: res } = await addCategories(
        this.categoriesId,
        this.addCategoriesForm
      );
      if (res.meta.status !== 201) return this.$message.error("添加参数错误！");
      this.$refs.cascaderhaha.handleChange();
      this.addCategoriesVisible = false;
    },
    //删除参数
    async deleteConfime(id) {
      var state = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除"
        });
      });
      // console.log(aa);
      if (state == "confirm") {
        var { data: res } = await deleteParams({
          id:  this.argument[this.argument.length - 1],
          attrid: id
        });
        if (res.meta.status !== 200) {
          this.$message.error("删除参数失败！");
        }
        this.$refs.cascaderhaha.handleChange();
      }
    },
    //对attr_vals操作保存到数据库中
    async saveAttrVals(row) {
      var { data: res } = await paramsList({
        id: this.categoriesId,
        sel: this.activeName
      });
    },
    //删除参数中的属性值
    handleClose(row, i) {
      //splice(索引号，几个) 从索引号多少开始删除几个
      row.attr_vals.splice(i, 1);
      this.editParamsConfirm(row);
    },
    //点击编辑显示对话框功能
    editConfime(row) {
      this.editForm = row;
      this.editCategoriesVisible = true;
    },
    //点击确定按钮修改参数
    async editParamsConfirm(row) {
      // console.log(row);
      if (row.attr_vals.length > 0) {
        row.attr_vals = row.attr_vals.join(" ");
      } else {
        row.attr_vals = "";
      }
      var { data: res } = await editParams({
        id: row.cat_id,
        attrid: row.attr_id,
        attr_name: row.attr_name,
        attr_sel: row.attr_sel,
        attr_vals: row.attr_vals
      });
      // console.log(data);
      if (res.meta.status !== 200) {
        this.$message.error("修改参数失败！");
      }
      this.editCategoriesVisible = false;
      this.$refs.cascaderhaha.handleChange();
    }
  },
  //计算属性
  computed: {
    //按钮是否需要被禁用
    isBtnDisabled() {
      if (this.argument.length == 3) return false;
      return true;
    },
    //点击选项卡的不同获取到不同的参数
    titleText() {
      if (this.activeName === "many") {
        return "动态参数";
      }
      return "静态属性";
    }
  },
  components: {
    cascader
  }
};
</script>
<style scoped>
.el-breadcrumb {
  margin-bottom: 15px;
}
.el-alert {
  margin-bottom: 15px;
}
</style>



//原分类选项操作  现在分离了
//获取商品列表信息
    // async getCategoriesList() {
    //   var { data: res } = await categoriesList({ type: [3] });
    //   this.categoriesData = res.data;
    // },
    //获取选择参数的函数调用
    // async handleChange() {
    //   //选择分类获取参数
    //   if(this.argument.length !== 3){
    //     return this.argument = []
    //   }
    //   this.categoriesId = this.argument[this.argument.length - 1];
    //   var { data: res } = await paramsList({
    //     id: this.categoriesId,
    //     sel: this.activeName
    //   });
    //   if (res.meta.status !== 200) return this.$message.error("获取动态参数失败！");

    //   res.data.forEach(item => {
    //     item.inputVisible = false
    //     item.inputValue = ""
    //   });
    //   this.paramList = res.data;
    //   //把attr_vals字符串转换为数组类型
    //   this.paramList.forEach(item => {
    //     item.attr_vals ? (item.attr_vals = item.attr_vals.split(" ")) : [];
    //   });
    //   console.log(this.paramList, 95);
    // },
    //生命周期
  // created() {
  //   this.getCategoriesList();
  // },